package com.lagou.rpc.consumer;


import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.pojo.User;
import com.lagou.rpc.zookeeper.ZookeeperClient;
import com.lagou.rpc.zookeeper.config.ZookeeperConfig;

import java.util.HashMap;
import java.util.Map;

public class ClientBootStrap {
    //zookeeper连接地址
    private static final String ZOOKEEPER_ADDRESS = "106.75.185.109:2181";

    public static void main(String[] args) throws InterruptedException {
        Map<String, Object> instanceCacheMap = new HashMap<>();
        instanceCacheMap.put(IUserService.class.getName(), IUserService.class);
        //初始化zookeeper配置
        //封装服务端zookeeper配置信息
        ZookeeperConfig zookeeperConfig = ZookeeperConfig.getInstance();
        zookeeperConfig.setZookeeperAddress(ZOOKEEPER_ADDRESS);
        zookeeperConfig.setConsumerSide(true);
        //每五秒中上报一次
        zookeeperConfig.setInterval(5);
        //初始化连接zookeeper客户端
        ZookeeperClient zookeeperClient = new ZookeeperClient(zookeeperConfig);

        //获取服务端节点信息
        Consumer consumer = new Consumer(zookeeperClient, instanceCacheMap);

        IUserService userService = (IUserService) consumer.createProxy(IUserService.class);
        while (true) {
            Thread.sleep(3000);
            try {
                User user = userService.getById(1);
                System.out.println(user);
                System.out.println("======================================================================================================================================================");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
